/*
  Copyright (c) 2009-10 by kRaw1er & HierOS
  This file is part of M8J project
*/
#pragma once

#include <mzfc_inc.h>
#include <ShellNotifyMsg.h>
#include <ReadWriteIni.h>
#include "utils\utils.h"
#include "typedefs.h"
#include "gloox\stanza.h"
#include "gloox\rosterlistener.h"
#include "gloox\messagehandler.h"
using namespace gloox;

class CM8JMainWnd;

class CContactList : public UiList, public RosterListener, public MessageHandler
{
public:
	CContactList(CM8JMainWnd * parent);
	~CContactList();

	//*******************************************************************************
	// UiList
	virtual void OnRemoveItem(int nIndex);
	virtual void DrawItem(HDC hdcDst, int nIndex, RECT * prcItem, RECT * prcWin, RECT * prcUpdate);
	//*******************************************************************************

	//*******************************************************************************
	// Roster listener
	virtual void handleRoster(const Roster &roster);
	virtual void handleItemAdded (const JID &jid);
	virtual void handleItemSubscribed (const JID &jid);
	virtual void handleItemRemoved (const JID &jid);
	virtual void handleItemUpdated (const JID &jid);
	virtual void handleItemUnsubscribed (const JID &jid);
	virtual void handleRosterPresence (const RosterItem &item, const std::string &resource, Presence presence, const std::string &msg);
	virtual void handleSelfPresence (const RosterItem &item, const std::string &resource, Presence presence, const std::string &msg);
	virtual bool handleSubscriptionRequest (const JID &jid, const std::string &msg);
	virtual bool handleUnsubscriptionRequest (const JID &jid, const std::string &msg);
	virtual void handleNonrosterPresence (Stanza *stanza);
	virtual void handleRosterError (Stanza *stanza);
	//********************************************************************************

	//*******************************************************************************
	// Message handler
	virtual void handleMessage(Stanza * stanza, MessageSession * session);
	//********************************************************************************

	ContactsMap::iterator SearchJidWoCaseSens(const std::string &jid);
	void SetSelectedIndex(int nIndex);
	void SetNewContactJid(const std::string &new_jid);
	void SetNewContactJid(ContactsMap::iterator &pos, const std::string &new_jid);
	const bool IsTwiceSelected();
	void ClearAllMessages();
	void ClearJidMessages(int nIndex);
	void ClearJidMessages(const std::string &jid);
	void CContactList::ClearJidMessages(ContactsMap::iterator pos);
	void OnNewMessage(const std::string &jid, const std::wstring &message, MessageType MessageType);
	HANDLE hr;

private:
	ContactsMap m_ContactsMap;
	GroupsMap m_GroupsMap;
	CM8JMainWnd * m_Parent;

	void AddContactToContactList(const std::string &jid, RosterItem * pRosterItem);
	void AddContactToGroup(CContactItemData * pItemData, const std::string &group_name);
	void AddContactToGroup(CContactItemData * pItemData, const StringList &groups);

	void RemoveItemFromContactsMap(const std::string &jid);
};

